// Package classification Tax API.
//
//
//     Schemes: http, https
//     Version: 0.0.1
//     Contact: cjp<3457911287@qq.com>
//
//     Consumes:
//     - application/json
//     - application/xml
//
//     Produces:
//     - application/json
//     - application/xml
//
// swagger:meta
package src

import (
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	. "myTaxServer/src/dataBase"
	. "myTaxServer/src/register"
	. "myTaxServer/src/structs"
	. "myTaxServer/src/tools"
	"net/http"
)

// Get serves the API for this get index page
func UserLogin(c *gin.Context){
	// swagger:route POST /api/Tax/Login/userLogin Login userLogin
	//
	//     Consumes:
	//     - application/json
	//
	//     Produces:
	//     - application/json
	//
	//     Schemes: http, https, ws, wss
	//
	//     Responses:
	//       200: history
	str := ByteToString(c);//转化成字符串
	var strr UserInfo;//把获取到的字符串转化成结构体的
	Err = json.Unmarshal([]byte(str), &strr);
	if Err != nil {
		fmt.Println("error:", Err);
	}
	strrr := GetSessiocKey(strr.Code)//获取session_Key
	userInfo := *Jiemi(strrr.Session_key,strr.Iv,strr.EncryptedData)//解密用户数据
	InsertUserInfo(userInfo,strr.Code);//插入用户数据（这里会判断用户是否已经注册过）
	c.JSON(http.StatusOK, gin.H{
		"OpenID": userInfo.OpenID,//给客户端返回解密的用户数据
		"UnionID": userInfo.UnionID,
		"NickName": userInfo.NickName,
	})
}

func InsertDatas(c *gin.Context){
	// swagger:route POST /api/Tax/Compute/result Compute result
	//
	//     Consumes:
	//     - application/json
	//
	//     Produces:
	//     - application/json
	//
	//     Schemes: http, https, ws, wss
	//
	//     Responses:
	//       200: result
	//Datas := c.Request.FormValue("userdatas")
	str := ByteToString(c);//将数据转化成字符串
	var strr MoneyData;//这里是转化成结构体的
	Err = json.Unmarshal([]byte(str), &strr);
	if Err != nil {
		fmt.Println("error:", Err);
	}
	temp := Result(strr); //将参数传递到result函数
	Ip := c.ClientIP();//获取ip
	StorageUserDatas(strr,temp,Ip) //存到用户查询记录里面
	c.JSON(http.StatusOK, gin.H{//返回用户计算的结果
		"data": temp,
	})
}

// Get serves the API for this get index page
func FindMyHistory(c *gin.Context)  {
	// swagger:route GET /api/Tax/History/findMyHistory History findMyHistory
	//
	//     Consumes:
	//     - application/json
	//
	//     Produces:
	//     - application/json
	//
	//     Schemes: http, https, ws, wss
	//
	//     Responses:
	//       200: history
	openid :=c.Query("openId");
	usershistory := FindHistory(openid);//返回一个历史记录切片
	c.JSON(http.StatusOK,gin.H{
		"history":usershistory,
	})
}

func FindHistoryResult(c *gin.Context){
	// swagger:route GET /api/Tax/History/findHistoryResult History findHistoryResult
	//
	//     Consumes:
	//     - application/json
	//
	//     Produces:
	//     - application/json
	//
	//     Schemes: http, https, ws, wss
	//
	//     Responses:
	//       200: historyResult
	userQueryId :=c.Query("userQueryId");
	returnData := FindResult(userQueryId)//返回查询到的结果
	c.JSON(http.StatusOK,gin.H{
		"result":returnData,
	})
}